<script setup lang="ts">
import {useRoute} from "vue-router";
import {Plus} from "@element-plus/icons-vue";
import request from "@/utils/request";
import {ref} from "vue";
import {ElMessage} from "element-plus";
import {changePlat, getPlatuser} from "@/api/system/plat-user";
const rules = ref({
    title: [{
        required: true,
        message: '请输入权益名称',
        trigger: 'blur'
    }],
    content: [{
        required: true,
        message: '请输入权益内容',
        trigger: 'blur'
    }],
});
const elRef = ref(null);
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`);
    showListData.value.pageNum = val;
    EquityListInfo.value.length = 0;
    getEquityList(showListData.value).then(res => {
        console.log(res);
        EquityListInfo.value?.push((res as any).rows);
    });
};
const route = useRoute();
const routes = route.query.userId;

//获取权益列表
const EquityListInfo = ref<object[]>([]);

function getEquityList(query) {
    return request({
        url: '/jsMemberBenefits/page',
        method: 'get',
        params: query,
    });
}

const appInfototal = ref(0);
const showListData = ref({
    jsUserId: routes,
    pageNum: 1,
    pageSize: 10,

});
getEquityList(showListData.value).then(res => {
    EquityListInfo.value.push((res as any).rows);
    appInfototal.value = (res as any).total;
});

//=新增权益====================
const addEquityData=ref({
    jsUserId:routes,
    title:'',
    content:''
});
const addFormVisible=ref(false);
function addEquity() {
    addFormVisible.value = true;
}
function addEquityFn(query){
    return request({
        url: '/jsMemberBenefits',
        method: 'post',
        data: query,
    });
}
function changePlatForm() {
    (elRef as any).value.validate((valid) => {
        if (valid) {
            addEquityFn(addEquityData.value).then(() => {
                ElMessage.success('修改成功');
                (EquityListInfo as any).value.length=0;
                getEquityList(showListData.value).then(res => {
                    EquityListInfo.value.push((res as any).rows);
                    appInfototal.value = (res as any).total;
                });
                addFormVisible.value = false;
            });

        } else {
            ElMessage.error('提交失败');
            return false;
        }
    });
};
//编辑权益===============================================
const changeFormVisible=ref(false);
const changeEquityData=ref({
    memberBenefitsId:'',
    jsUserId:routes,
    title:'',
    content:''
});
function changeFn(query){
    return request({
        url: `/jsMemberBenefits`,
        method: 'put',
        data:query,
    });
}

function sureChange(){
    (elRef as any).value.validate((valid) => {
        if (valid) {
            changeFn(changeEquityData.value).then(() => {
                ElMessage.success('修改成功');
                (EquityListInfo as any).value.length=0;
                getEquityList(showListData.value).then(res => {
                    EquityListInfo.value.push((res as any).rows);
                    appInfototal.value = (res as any).total;
                });
                changeFormVisible.value = false;
            });

        } else {
            ElMessage.error('提交失败');
            return false;
        }
    });
}
function getOnceInfo(id){
    return request({
        url: `/jsMemberBenefits/${id}`,
        method: 'get',
    });
}
function changeEquity(id){
    changeEquityData.value.memberBenefitsId=id;
    changeFormVisible.value=true;
    getOnceInfo(id).then(res=>{
        console.log(res);
        changeEquityData.value.title=res.data.title;
        changeEquityData.value.content=res.data.content;
    });
}
//====================================删除权益
function deleteEquityFn(deleteId) {
    return request({
        url: '/jsMemberBenefits?memberBenefitsIdList=' + deleteId,
        method: 'delete',
    });
}
const deleteEquityId=ref();
function deleteEquity(id) {
    deleteEquityId.value=id;
    deleteDialogVisible.value = true;
}

const deleteDialogVisible = ref(false);

function cencelDelete() {
    addFormVisible.value = false;
    deleteDialogVisible.value = false;
    changeFormVisible.value=false;
    ElMessage('取消操作');
}

function sureDelete() {
    deleteEquityFn(deleteEquityId.value).then(()=>{
        ElMessage.success('删除成功');
        deleteDialogVisible.value = false;
        (EquityListInfo as any).value.length=0;
        getEquityList(showListData.value).then(res => {
            EquityListInfo.value.push((res as any).rows);
            appInfototal.value = (res as any).total;
        });
    });
}
</script>

<template>
    <div>
        <section style="padding: 0 30px">
            <el-button type="primary" :icon="Plus" style="margin-top: 20px;" @click="addEquity">新增</el-button>
            <el-table :data="(EquityListInfo[0] as any)" style="width: 100%;margin-top: 50px" stripe>
                <template #empty>
                    <el-empty description="暂无数据"/>
                </template>
                <el-table-column prop="title" label="权益名称" height/>
                <el-table-column prop="content" label="权益内容" height/>
                <el-table-column prop="操作" label="操作" height>
                    <template #default="index">
                        <button style="color: #1c84c6" @click="changeEquity(index.row.memberBenefitsId)">编辑</button>
                        <button style="color: #1c84c6;margin-left: 20px" @click="deleteEquity(index.row.memberBenefitsId)">删除</button>
                    </template>
                </el-table-column>
            </el-table>
        </section>
        <div style="position: relative">
            <span style="margin-left: 1000px;position: absolute;top: 8px">共{{ appInfototal }}条数据</span>
            <el-pagination
                style="margin-top: 20px;margin-left: 1150px"
                :page-size="showListData.pageSize"
                layout="prev, pager, next"
                :total="appInfototal"
                @currentChange="handleCurrentChange"
            />
        </div>
        <el-dialog v-model="deleteDialogVisible" title="提示" width="500" center>

            <el-icon color="rgb(255, 186, 0)" size="25">
                <Warning/>
            </el-icon>
            <span style="font-size: 20px;vertical-align: top;margin-left: 10px">进行删除操作后无法恢复，是否继续删除？</span>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="cencelDelete">取消</el-button>
                    <el-button type="primary" @click="sureDelete">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <el-dialog v-model="addFormVisible" title="新增" width="600" style="padding: 20px">
            <el-form ref="elRef" :model="addEquityData" :rules="rules">
                <el-form-item label="权益名称：" prop="title">
                    <el-input
                        v-model="addEquityData.title"
                        placeholder="请输入权益名称"
                        clearable
                        size="large"></el-input>
                </el-form-item>
                <el-form-item label="权益内容：" prop="content">
                    <el-input
                        v-model="addEquityData.content"
                        placeholder="请输入权益内容"
                        clearable
                        size="large"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer" style="margin-left: 360px">
                    <el-form-item>
                        <el-button size="large" @click="cencelDelete">取消</el-button>
                        <el-button type="primary" size="large" @click="changePlatForm">确定</el-button>
                    </el-form-item>
                </div>
            </template>
        </el-dialog>
        <el-dialog v-model="changeFormVisible" title="修改" width="600" style="padding: 20px">
            <el-form ref="elRef" :model="changeEquityData" :rules="rules">
                <el-form-item label="权益名称：" prop="title">
                    <el-input
                        v-model="changeEquityData.title"
                        placeholder="请输入权益名称"
                        clearable
                        size="large"></el-input>
                </el-form-item>
                <el-form-item label="权益内容：" prop="content">
                    <el-input
                        v-model="changeEquityData.content"
                        placeholder="请输入权益内容"
                        clearable
                        size="large"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer" style="margin-left: 360px">
                    <el-form-item>
                        <el-button size="large" @click="cencelDelete">取消</el-button>
                        <el-button type="primary" size="large" @click="sureChange">确定</el-button>
                    </el-form-item>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<style scoped>
/deep/ .cell {
    text-align: center !important;
}
</style>
